package controller;

import common.OrderComment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Food;
import pojo.FoodCate;
import pojo.Restaurant;
import service.ICateService;
import service.IFoodService;
import service.IRestService;
import service.IUserService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/rest")
public class RestController {
    private Map<String, Object> responseMsg = new HashMap<>();

    @Resource
    IRestService restService;

    @Resource
    IFoodService foodService;

    @Resource
    ICateService cateService;


    @Resource
    IUserService userService;

    /**
     * 获取店铺详情
     *
     * @param id 店铺id
     * @return 店铺对象
     */
    @RequestMapping("/info")
    @ResponseBody
    public Map<String, Object> restInfo(@RequestParam Integer id) {
        responseMsg.clear();
        Restaurant restaurant = restService.getRestInfo(id);
        if (restaurant != null) {
            responseMsg.put("success", true);
            responseMsg.put("message", "get restaurant info success");
            responseMsg.put("restaurant", restaurant);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "get restaurant info failure");
        }
        return responseMsg;
    }

    @RequestMapping("/init")
    @ResponseBody
    public Map<String, Object> restInfoInit(@RequestParam Integer id,@RequestParam Integer userId) {
        responseMsg.clear();
        Restaurant restaurant = restService.getRestInfo(id);
        List<Food> foodList = foodService.listFood(id);
        List<FoodCate> cateList = cateService.listCate(id);
        List<OrderComment> commentList = restService.getRestComments(id);
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        map.put("restaurantId",id);
        boolean isCollection=userService.selectSubscribeTotal(map)==0?false:true;

        if (restaurant != null) {
            responseMsg.put("success", true);
            responseMsg.put("message", "init restaurant info success");
            responseMsg.put("restaurant", restaurant);
            responseMsg.put("food_list", foodList);
            responseMsg.put("cate_list", cateList);
            responseMsg.put("rest_comments_list", commentList);
            responseMsg.put("isCollection", isCollection);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "init restaurant info failure");
        }
        return responseMsg;
    }

    /**
     * 获取营业店铺列表
     *
     * @return 店铺列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> listRest(@RequestParam String query,
                                        @RequestParam int pagenum,
                                        @RequestParam int pagesize) {
        responseMsg.clear();
        Map<String, Object> map = new HashMap<>();

        map.put("query", '%'+query+'%');
        map.put("pagenum", (pagenum - 1) * pagesize);
        map.put("pagesize", pagesize);
        map.put("status", (byte) 2);

        List<Restaurant> list = restService.listOpen(map);
        int total = restService.selectTotal(map);

        if (!list.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "list restaurant success");
            responseMsg.put("rest_list", list);
            responseMsg.put("total", total);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "list restaurant failure");
        }
        return responseMsg;
    }

    /**
     * 确认开店
     *
     * @param id 开店店铺id
     * @return 改变店铺状态结果
     */
    @RequestMapping("/open")
    @ResponseBody
    public Map<String, Object> openRest(@RequestParam String id) {
        responseMsg.clear();
        Integer restId = Integer.parseInt(id);
        // 改变店铺状态为开店3
        int result = restService.setStatus(restId, (byte) 2);
        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "open restaurant success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "open restaurant failure");
        }
        return responseMsg;
    }

    /**
     * 获取当前用户的申请店铺
     *
     * @param uid userId
     * @return 改变店铺状态结果
     */
    @RequestMapping("/myrest")
    @ResponseBody
    public Map<String, Object> getMyRest(@RequestParam Integer uid) {
        responseMsg.clear();
        Restaurant rest = restService.getMyRest(uid);
        if (rest != null) {
            responseMsg.put("success", true);
            responseMsg.put("message", "get my restaurant success");
            responseMsg.put("my_rest", rest);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "get my restaurant failure");
        }
        return responseMsg;
    }

    /**
     * 修改店铺信息
     *
     * @param restaurant 店铺对象
     * @return 修改结果
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> updateRest(@RequestBody Restaurant restaurant) {
        responseMsg.clear();
        int result = restService.updateRest(restaurant);
        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "update restaurant success");
        } else {
            responseMsg.put("success", true);
            responseMsg.put("message", "update restaurant failure");
        }
        return responseMsg;
    }

    /**
     * 店铺加盟
     *
     * @param restaurant 店铺对象
     * @return 加盟结果
     */
    @RequestMapping("/register")
    @ResponseBody
    public Map<String, Object> restRegister(@RequestBody Restaurant restaurant) {
        responseMsg.clear();
        restaurant.setStatus((byte) 0);
        int result = restService.register(restaurant);
//        System.out.println(restaurant.getCreateTime());

        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "register restaurant success");
        } else {
            responseMsg.put("success", true);
            responseMsg.put("message", "register restaurant failure");
        }
        return responseMsg;
    }

    /**
     * 获取店铺的相关评价
     *
     * @param rid 店铺id
     * @return 评价列表
     */
    @RequestMapping("/comments")
    @ResponseBody
    public Map<String, Object> restComments(@RequestParam Integer rid) {
        responseMsg.clear();
        List<OrderComment> list = restService.getRestComments(rid);
        if (!list.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "get restaurant comments success");
            responseMsg.put("rest_comments_list", list);
        } else {
            responseMsg.put("success", true);
            responseMsg.put("message", "get restaurant comments failure");
        }

        return responseMsg;
    }
}
